app.controller('pictureCtrl',['$scope','mainService',function($scope,mainService) {
    $scope.$on('nmb', function (event,data ) {

    });
    $scope.picPath = mainService.getUrl().src;
    console.log('pic')
    console.log(mainService.getUrl())
    var file = $_('file').files[0];
    console.log(file)
    // if(mainService.getUrl().name){
    //     file.name = mainService.getUrl().name
    // }
    // if(mainService.getUrl().src){
    //     $scope.picPath = mainService.getUrl().src;
    // }

    document.getElementById("file").onchange = function (event) {
        gen_base64();
        var e=window.event||event;
        // 获取当前选中的文件
        var oFile = e.target.files[0];
        console.log(oFile);//打印值看下面图片,简单点的话我们直接把这个数据给后台处理就可以了
        mainService.setFile(oFile)
    };
    function $_(id) {
        return document.getElementById(id);
    }
    function gen_base64() {
        var file = $_('file').files[0];

        r = new FileReader();  //本地预览
        r.onload = function(){
           mainService.setUrl({
               src:r.result,
               name:file.name
           });
           document.getElementById('showPic').src = r.result;
            $scope.picPath = r.result;

        };
        r.readAsDataURL(file);    //Base64
    }
    // function getBase64Image(img) {
    //     var canvas = document.createElement("canvas");
    //     canvas.width = img.width;
    //     canvas.height = img.height;
    //     var ctx = canvas.getContext("2d");
    //     ctx.drawImage(img, 0, 0, img.width, img.height);
    //     var ext = img.src.substring(img.src.lastIndexOf(".")+1).toLowerCase();
    //     var dataURL = canvas.toDataURL("image/"+ext);
    //     return dataURL;
    // }
    $scope.openBrowse  = function (){
        var ie=navigator.appName=="Microsoft Internet Explorer" ? true : false;
        if(ie){
            document.getElementById("file").click();
            document.getElementById("filename").value=document.getElementById("file").value;
        }else{
            var a=document.createEvent("MouseEvents");//FF的处理
            a.initEvent("click", true, true);
            document.getElementById("file").dispatchEvent(a);
        }
    };

}]);